// pages/shop/withdraw.js
var app = getApp();
var curPage = 1;
Page({
  data: {
    withDrawtypes: [
      {
        types: 1,
        names: '提现'
      },
      {
        types: 2,
        names: '返利'
      }
    ],
    logs: [],
    more: true,
    navbar: [{
      status: 2,
      name: '已申请'
    }, {
      status: 3,
      name: '已提现'
    }, {
      status: 4,
      name: '已返利'
    }],
    logStatus: ['已收入', '已申请', '已提现', '已拒绝'],
    navbarIndex: 0,
    showTips: false
  },
  onLoad: function (options) {
    var that = this;
    this.setData({
      windowHeight: app.systemInfo.windowHeight
    })
    this.reLoad();
    app.util.appInfo(app, function(res){
      var withDrawtypes = that.data.withDrawtypes;
      that.setData({
        app_kfphone: res.result.kfphone,
        app_kfwechat: res.result.kfwechat,
        app_withdraw_min_amount: res.result.withdraw_min_amount,
        app_withdraw_daily_times: res.result.withdraw_daily_times,
        app_withdraw_max_amount: res.result.withdraw_max_amount,
        is_alipay: res.result.alipay_switch
      })
    });
  },
  reLoad: function() {
    var that = this;
    var curStatus = that.data.navbar[that.data.navbarIndex].status;
    this.getLogs(1, curStatus);
    curPage = 1;
    app.func.req('shops/show', {
      id: wx.getStorageSync('shop').id,
      is_withdraw: true
    }, 'GET', function (res) {
      var item = res.result;
      if (item.amount == null) {
        item.amount = 0;
      }
      item.rate = (item.rate / 10).toFixed(1);
      that.setData({
        shop: item
      });
      wx.setStorageSync('shop', item)
    }, {
        'token': wx.getStorageSync('token')
      });
  },
  withDraw: function (e) {
    var that = this
    var shop = this.data.shop;
    var formId = this.data.formId;
    var types = this.data.withDrawtypes[e.detail.value].types;
    var pay_types = 1;
    if (pay_types == 2 && (!shop.alipay_account || !shop.alipay_name)){
      wx.showToast({
        title: '请绑定支付宝',
        image: '/images/fail.png',
        duration: 2000
      })
      return
    } else if (pay_types == 1 && !shop.wxpay_account){
      wx.showToast({
        title: '请绑定微信号',
        image: '/images/fail.png',
        duration: 2000
      })
      return
    }
    app.func.req('shops/withdraw', {
      shop_id: shop.id,
      types: types,
      pay_types: pay_types,
      form_id: formId
    }, 'POST', function (res) {
      if (res.code == "success") {
          var title = '已提交，等待处理'
          if (res.msg == "shop.withdraw.success") {
            title = '提现已成功'
          }
          if (types == 1 && parseFloat(that.data.shop.amount) > parseFloat(that.data.app_withdraw_max_amount)) {
            wx.showModal({
              title: '提示',
              content: '本次提款超过限额，将为您提取' + that.data.app_withdraw_max_amount + '元',
              showCancel: false,
              success: function (res) {
                wx.showToast({
                  title: title,
                  icon: 'success',
                  duration: 2000
                })
                setTimeout(function () {
                  that.reLoad()
                }, 2000)
              }
            })
          } else if (types == 2 && parseFloat(that.data.shop.brokerage) > parseFloat(that.data.app_withdraw_max_amount)) {
            wx.showModal({
              title: '提示',
              content: '本次提款超过限额，将为您提取' + that.data.app_withdraw_max_amount + '元',
              showCancel: false,
              success: function (res) {
                wx.showToast({
                  title: title,
                  icon: 'success',
                  duration: 2000
                })
                setTimeout(function () {
                  that.reLoad()
                }, 2000)
              }
            })
          } else {
            wx.showToast({
              title: title,
              icon: 'success',
              duration: 2000
            })
            setTimeout(function () {
              that.reLoad()
            }, 2000)
          }
      } else {
        var error_title;
        if (res.msg == "withdraw.shop.invalid") {
          error_title = "最低提现金额" + that.data.app_withdraw_min_amount + "元"
        } else if (res.msg == "shop.withdraw.too_many_times_today") {
          error_title = "每日最多提现"+ that.data.app_withdraw_daily_times + "次"
        } else {
          error_title = "提交申请失败"
        }
        wx.showModal({
          title: '提示',
          content: error_title,
          showCancel: false,
          confirmText: '朕知道了',
        })
      }
    }, {
        token: wx.getStorageSync('token')
      })
  },
  setformId: function (e) {
    this.setData({
      formId: e.detail.formId
    })
  },
  telPhone: function () {
    var that = this;
    wx.makePhoneCall({
      phoneNumber:  that.data.app_kfphone
    })
  },
  getLogs: function (page, status) {
    var that = this;
    wx.showLoading({
      title: '记录加载中...',
      mask: true
    });
    var req = {
      page: page,
      status: status,
    }
    if (status == 0) {
      req = {
        page: page,
      };
    }
    app.func.req('shops/get_logs', req, 'GET', function (res) {
      var logs = res.result;
      for (var i = 0; i < logs.length; i++) {
        logs[i].status_alias = that.data.logStatus[logs[i].status - 1]
      }
      var curLogs = logs;
      if (page > 1) {
        curLogs = that.data.logs.concat(logs);
      }
      that.setData({
        logs: curLogs
      })
      if (res.result.length == 0) {

        that.setData({
          more: false
        })
      }
      wx.hideLoading();
    }, {
        token: wx.getStorageSync('token')
      })
  },
  selectStatus: function (e) {
    var that = this;
    var index = e.target.dataset.index;
    this.setData({
      navbarIndex: index,
      more: true,
      scrollTop: 0
    })

    var curStatus = that.data.navbar[index].status;
    this.getLogs(1, curStatus);
    curPage = 1;
  },
  loadMore: function () {
    var that = this;
    var curStatus = that.data.navbar[that.data.navbarIndex].status;
    var more = this.data.more;
    curPage++;
    if (more) {
      this.getLogs(curPage, curStatus);
    }
  },
  showTips: function () {
    this.setData({
      showTips: !this.data.showTips
    })
  }
})
